<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="STM32CubeMX W25Q64">
    <meta name="description" content="本篇详细的记录了如何使用STM32CubeMX配置 STM32G070RBT6 的硬件SPI外设与 SPI Flash 通信（W25Q64）。


1. 准备工作硬件准备
开发板首先需要准备一个开发板，这里我准备的是STM32G070RB的">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>STM32CubeMX | 30-使用硬件SPI读写Flash(W25Q64) | Mculover666</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>
    
<meta name="generator" content="Hexo 4.2.1"><link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">Mculover666</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-list" style="zoom: 0.6;"></i>
      
      <span>系列教程</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/posts/578764034/">
          
          <span>STM32CubeMX</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="https://blog.csdn.net/Mculover666" target="_blank" rel="noopener" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>CSDN</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/download" class="waves-effect waves-light">
      
      <i class="fas fa-download" style="zoom: 0.6;"></i>
      
      <span>资源下载</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">Mculover666</div>
        <div class="logo-desc">
            
            嵌入式 STM32 物联网领域的教程及精彩内容分享
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-list"></i>
			
			系列教程
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>   
				
                  <a href="/posts/578764034/ " style="margin-left:75px";>
				  
		          <span>STM32CubeMX</span>
                  </a>
                </li>
               
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="https://blog.csdn.net/Mculover666" target="_blank" rel="noopener" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			CSDN
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/download" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-download"></i>
			
			资源下载
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/Mculover666" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>

        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/Mculover666" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/12.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">STM32CubeMX | 30-使用硬件SPI读写Flash(W25Q64)</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        height: calc(100vh - 250px);
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/STM32CubeMX-W25Q64/">
                                <span class="chip bg-color">STM32CubeMX W25Q64</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/STM32CubeMX/" class="post-category">
                                STM32CubeMX
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-07-26
                </div>
                

                

                

                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>

        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <p>本篇详细的记录了如何使用STM32CubeMX配置 STM32G070RBT6 的硬件SPI外设与 SPI Flash 通信（W25Q64）。</p>
<a id="more"></a>

<h1 id="1-准备工作"><a href="#1-准备工作" class="headerlink" title="1. 准备工作"></a>1. 准备工作</h1><h2 id="硬件准备"><a href="#硬件准备" class="headerlink" title="硬件准备"></a>硬件准备</h2><ul>
<li><p>开发板<br>首先需要准备一个开发板，这里我准备的是STM32G070RB的开发板</p>
</li>
<li><p>SPI Flash<br>开发板板载一片SPI Flash，型号为 <code>W25Q64JV</code>，大小为 8 MB。</p>
</li>
</ul>
<h2 id="软件准备"><a href="#软件准备" class="headerlink" title="软件准备"></a>软件准备</h2><ul>
<li>需要安装好Keil - MDK及芯片对应的包，以便编译和下载生成的代码；</li>
<li>准备一个串口调试助手，这里我使用的是<code>Serial Port Utility</code>；</li>
</ul>
<blockquote>
<p>Keil MDK和串口助手Serial Port Utility 的安装包都可以<strong>在文末关注公众号获取</strong>，回复关键字获取相应的安装包：</p>
</blockquote>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwODE0L2d1YmFPd21FVHAxdy5wbmc?x-oss-process=image/format,png" alt="mark"></p>
<h1 id="2-生成MDK工程"><a href="#2-生成MDK工程" class="headerlink" title="2.生成MDK工程"></a>2.生成MDK工程</h1><h2 id="选择芯片型号"><a href="#选择芯片型号" class="headerlink" title="选择芯片型号"></a>选择芯片型号</h2><p>打开STM32CubeMX，打开MCU选择器：<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwODA2L2dCUDZnbG1VU0g4MC5wbmc?x-oss-process=image/format,png" alt=""></p>
<p>搜索并选中芯片<code>STM32G070RB</code>:<br><img src="https://img-blog.csdnimg.cn/20200726092835777.png" alt=""></p>
<h2 id="配置时钟源"><a href="#配置时钟源" class="headerlink" title="配置时钟源"></a>配置时钟源</h2><ul>
<li>如果选择使用外部高速时钟（HSE），则需要在System Core中配置RCC；</li>
<li>如果使用默认内部时钟（HSI），这一步可以略过；</li>
</ul>
<p>这里我都使用内部时钟：<br><img src="https://img-blog.csdnimg.cn/20200726092928549.png" alt=""></p>
<h2 id="配置串口"><a href="#配置串口" class="headerlink" title="配置串口"></a>配置串口</h2><p>开发板板载了一个CH340z换串口，连接到USART1。</p>
<p>接下来开始配置<code>USART1</code>：<br><img src="https://img-blog.csdnimg.cn/20200726093117174.png" alt=""></p>
<h2 id="配置SPI接口"><a href="#配置SPI接口" class="headerlink" title="配置SPI接口"></a>配置SPI接口</h2><p>开发板上SPI Flash的原理图如下：<br><img src="https://img-blog.csdnimg.cn/20200726092623733.png#pic_center" alt=""></p>
<blockquote>
<p>原理图中虽然将CS片选接到了硬件SPI1的NSS引脚，因为硬件NSS使用比较麻烦，所以后面直接把PA4配置为普通GPIO，手动控制片选信号。</p>
</blockquote>
<p>接下来配置 SPI1 接口。</p>
<p>配置SPI接口的时候有三个需要注意的点：</p>
<p>① 分频系数；<br>② CPOL：CLK空闲时候的电平为高电平或者低电平；<br>③ CPHA：在第1个时钟边缘采样，还是在第2个时钟边缘采样；</p>
<p>首先配置硬件SPI的模式：<br><img src="https://img-blog.csdnimg.cn/20200726093714976.png" alt=""><br>接着根据W25Q64数据手册中给出的通信协议，配置SPI外设具体参数：<br><img src="https://img-blog.csdnimg.cn/20200726093903610.png" alt=""><br>其实CPOL参数和CPHA参数，从W25Q64中随意找个时序图就可以看出，比如我以读取ID的时序为例：<br><img src="https://img-blog.csdnimg.cn/20200726094226471.png" alt=""></p>
<h2 id="配置时钟树"><a href="#配置时钟树" class="headerlink" title="配置时钟树"></a>配置时钟树</h2><p>STM32G070RB的最高主频到64M，所以配置PLL，最后使<code>HCLK = 64Mhz</code>即可：<br><img src="https://img-blog.csdnimg.cn/20200726094317380.png" alt=""></p>
<h2 id="生成工程设置"><a href="#生成工程设置" class="headerlink" title="生成工程设置"></a>生成工程设置</h2><p><img src="https://img-blog.csdnimg.cn/20200726094350788.png" alt=""></p>
<h2 id="代码生成设置"><a href="#代码生成设置" class="headerlink" title="代码生成设置"></a>代码生成设置</h2><p>最后设置生成独立的初始化文件：<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwODA2L1Q2V3ZTSzZEZnB0cy5wbmc?x-oss-process=image/format,png" alt=""></p>
<h2 id="生成代码"><a href="#生成代码" class="headerlink" title="生成代码"></a>生成代码</h2><p>点击<code>GENERATE CODE</code>即可生成MDK-V5工程：</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwODA2L3MwakdoTEJXVzZDbS5wbmc?x-oss-process=image/format,png" alt=""></p>
<h1 id="3-重定向printf函数到USART1"><a href="#3-重定向printf函数到USART1" class="headerlink" title="3. 重定向printf函数到USART1"></a>3. 重定向printf函数到USART1</h1><p>参考：<a href="http://www.mculover666.cn/posts/2251182441/">【STM32Cube_09】重定向printf函数到串口输出的多种方法</a>。</p>
<h1 id="4-封装-SPI-Flash（W25Q64）的命令和底层函数"><a href="#4-封装-SPI-Flash（W25Q64）的命令和底层函数" class="headerlink" title="4. 封装 SPI Flash（W25Q64）的命令和底层函数"></a>4. 封装 SPI Flash（W25Q64）的命令和底层函数</h1><p>MCU 通过向 SPI Flash <strong>发送各种命令</strong> 来读写 SPI Flash内部的寄存器，所以这种裸机驱动，首先要先宏定义出需要使用的命令，然后利用 HAL 库提供的库函数，封装出三个底层函数，<strong>便于移植</strong>：</p>
<ul>
<li>向 SPI Flash 发送数据的函数</li>
<li>从 SPI Flash 接收数据的函数</li>
<li>发送数据的同时读取数据的函数</li>
</ul>
<p>接下来开始编写代码~</p>
<h2 id="宏定义操作命令"><a href="#宏定义操作命令" class="headerlink" title="宏定义操作命令"></a>宏定义操作命令</h2><pre class=" language-c"><code class="language-c"><span class="token macro property">#<span class="token directive keyword">define</span> ManufactDeviceID_CMD    0x90</span>
<span class="token macro property">#<span class="token directive keyword">define</span> READ_STATU_REGISTER_1   0x05</span>
<span class="token macro property">#<span class="token directive keyword">define</span> READ_STATU_REGISTER_2   0x35</span>
<span class="token macro property">#<span class="token directive keyword">define</span> READ_DATA_CMD            0x03</span>
<span class="token macro property">#<span class="token directive keyword">define</span> WRITE_ENABLE_CMD        0x06</span>
<span class="token macro property">#<span class="token directive keyword">define</span> WRITE_DISABLE_CMD        0x04</span>
<span class="token macro property">#<span class="token directive keyword">define</span> SECTOR_ERASE_CMD        0x20</span>
<span class="token macro property">#<span class="token directive keyword">define</span> CHIP_ERASE_CMD            0xc7</span>
<span class="token macro property">#<span class="token directive keyword">define</span> PAGE_PROGRAM_CMD        0x02</span></code></pre>
<h2 id="封装发送数据的函数"><a href="#封装发送数据的函数" class="headerlink" title="封装发送数据的函数"></a>封装发送数据的函数</h2><pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/**
 * @brief    SPI发送指定长度的数据
 * @param    buf  —— 发送数据缓冲区首地址
 * @param    size —— 要发送数据的字节数
 * @retval   成功返回HAL_OK
 */</span>
<span class="token keyword">static</span> HAL_StatusTypeDef <span class="token function">SPI_Transmit</span><span class="token punctuation">(</span>uint8_t<span class="token operator">*</span> send_buf<span class="token punctuation">,</span> uint16_t size<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">HAL_SPI_Transmit</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>hspi1<span class="token punctuation">,</span> send_buf<span class="token punctuation">,</span> size<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="封装接收数据的函数"><a href="#封装接收数据的函数" class="headerlink" title="封装接收数据的函数"></a>封装接收数据的函数</h2><pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/**
 * @brief   SPI接收指定长度的数据
 * @param   buf  —— 接收数据缓冲区首地址
 * @param   size —— 要接收数据的字节数
 * @retval  成功返回HAL_OK
 */</span>
<span class="token keyword">static</span> HAL_StatusTypeDef <span class="token function">SPI_Receive</span><span class="token punctuation">(</span>uint8_t<span class="token operator">*</span> recv_buf<span class="token punctuation">,</span> uint16_t size<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
   <span class="token keyword">return</span> <span class="token function">HAL_SPI_Receive</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>hspi1<span class="token punctuation">,</span> recv_buf<span class="token punctuation">,</span> size<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="封装发送数据同时读取数据的函数"><a href="#封装发送数据同时读取数据的函数" class="headerlink" title="封装发送数据同时读取数据的函数"></a>封装发送数据同时读取数据的函数</h2><pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/**
 * @brief   SPI在发送数据的同时接收指定长度的数据
 * @param   send_buf  —— 接收数据缓冲区首地址
 * @param   recv_buf  —— 接收数据缓冲区首地址
 * @param   size —— 要发送/接收数据的字节数
 * @retval  成功返回HAL_OK
 */</span>
<span class="token keyword">static</span> HAL_StatusTypeDef <span class="token function">SPI_TransmitReceive</span><span class="token punctuation">(</span>uint8_t<span class="token operator">*</span> send_buf<span class="token punctuation">,</span> uint8_t<span class="token operator">*</span> recv_buf<span class="token punctuation">,</span> uint16_t size<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
   <span class="token keyword">return</span> <span class="token function">HAL_SPI_TransmitReceive</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>hspi1<span class="token punctuation">,</span> send_buf<span class="token punctuation">,</span> recv_buf<span class="token punctuation">,</span> size<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h1 id="5-编写W25Q64的驱动程序"><a href="#5-编写W25Q64的驱动程序" class="headerlink" title="5. 编写W25Q64的驱动程序"></a>5. 编写W25Q64的驱动程序</h1><p>接下来开始利用上一节封装的宏定义和底层函数，编写W25Q64的驱动程序：</p>
<h2 id="读取Manufacture-ID和Device-ID"><a href="#读取Manufacture-ID和Device-ID" class="headerlink" title="读取Manufacture ID和Device ID"></a>读取Manufacture ID和Device ID</h2><p>读取 Flash 内部这两个ID有两个作用：</p>
<ul>
<li>检测SPI Flash是否存在</li>
<li>可以根据ID判断Flash具体型号</li>
</ul>
<p>数据手册上给出的操作时序如图：</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwOTAzL0drZnZmZ1JnUkE1My5wbmc?x-oss-process=image/format,png" alt=""></p>
<p>根据该时序，编写代码如下：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/**
 * @brief   读取Flash内部的ID
 * @param   none
 * @retval  成功返回device_id
 */</span>
uint16_t <span class="token function">W25QXX_ReadID</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    uint8_t recv_buf<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>    <span class="token comment" spellcheck="true">//recv_buf[0]存放Manufacture ID, recv_buf[1]存放Device ID</span>
    uint16_t device_id <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    uint8_t send_data<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span>ManufactDeviceID_CMD<span class="token punctuation">,</span><span class="token number">0x00</span><span class="token punctuation">,</span><span class="token number">0x00</span><span class="token punctuation">,</span><span class="token number">0x00</span><span class="token punctuation">}</span><span class="token punctuation">;</span>   <span class="token comment" spellcheck="true">//待发送数据，命令+地址</span>

    <span class="token comment" spellcheck="true">/* 使能片选 */</span>
    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_RESET<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/* 发送并读取数据 */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>HAL_OK <span class="token operator">==</span> <span class="token function">SPI_Transmit</span><span class="token punctuation">(</span>send_data<span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>HAL_OK <span class="token operator">==</span> <span class="token function">SPI_Receive</span><span class="token punctuation">(</span>recv_buf<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            device_id <span class="token operator">=</span> <span class="token punctuation">(</span>recv_buf<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">&lt;&lt;</span> <span class="token number">8</span><span class="token punctuation">)</span> <span class="token operator">|</span> recv_buf<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/* 取消片选 */</span>
    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_SET<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> device_id<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="读取状态寄存器数据并判断Flash是否忙碌"><a href="#读取状态寄存器数据并判断Flash是否忙碌" class="headerlink" title="读取状态寄存器数据并判断Flash是否忙碌"></a>读取状态寄存器数据并判断Flash是否忙碌</h2><p>上文中提到，SPI Flash的所有操作都是靠发送命令完成的，但是 Flash 接收到命令后，需要一段时间去执行该操作，这段时间内 Flash 处于“忙”状态，MCU 发送的命令无效，不能执行，在 Flash 内部有2-3个状态寄存器，指示出 Flash 当前的状态，有趣的一点是：</p>
<p>当 Flash 内部在执行命令时，不能再执行 MCU 发来的命令，但是 MCU 可以一直读取状态寄存器，这下就很好办了，<strong>MCU可以一直读取，然后判断Flash是否忙完</strong>：</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwOTAzL05hTHQxVGJUTGtFUS5wbmc?x-oss-process=image/format,png" alt="mark"><br>读取协议如下：<br><img src="https://img-blog.csdnimg.cn/20200726095919624.png" alt=""></p>
<p>根据此协议实现的读取状态寄存器的代码如下：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/**
 * @brief     读取W25QXX的状态寄存器，W25Q64一共有2个状态寄存器
 * @param     reg  —— 状态寄存器编号(1~2)
 * @retval    状态寄存器的值
 */</span>
<span class="token keyword">static</span> uint8_t <span class="token function">W25QXX_ReadSR</span><span class="token punctuation">(</span>uint8_t reg<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    uint8_t result <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> 
    uint8_t send_buf<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0x00</span><span class="token punctuation">,</span><span class="token number">0x00</span><span class="token punctuation">,</span><span class="token number">0x00</span><span class="token punctuation">,</span><span class="token number">0x00</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">switch</span><span class="token punctuation">(</span>reg<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token number">1</span><span class="token punctuation">:</span>
            send_buf<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> READ_STATU_REGISTER_1<span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token number">2</span><span class="token punctuation">:</span>
            send_buf<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> READ_STATU_REGISTER_2<span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token number">0</span><span class="token punctuation">:</span>
        <span class="token keyword">default</span><span class="token punctuation">:</span>
            send_buf<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> READ_STATU_REGISTER_1<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

     <span class="token comment" spellcheck="true">/* 使能片选 */</span>
    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_RESET<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>HAL_OK <span class="token operator">==</span> <span class="token function">SPI_Transmit</span><span class="token punctuation">(</span>send_buf<span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>HAL_OK <span class="token operator">==</span> <span class="token function">SPI_Receive</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>result<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_SET<span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token keyword">return</span> result<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/* 取消片选 */</span>
    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_SET<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>然后编写<strong>阻塞判断</strong>Flash是否忙碌的函数：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/**
 * @brief    阻塞等待Flash处于空闲状态
 * @param   none
 * @retval  none
 */</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">W25QXX_Wait_Busy</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token function">W25QXX_ReadSR</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">&amp;</span> <span class="token number">0x01</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0x01</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 等待BUSY位清空</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="读取数据"><a href="#读取数据" class="headerlink" title="读取数据"></a>读取数据</h2><p>SPI Flash读取数据可以任意地址（地址长度32bit）读任意长度数据（最大 65535 Byte），没有任何限制，数据手册给出的时序如下：</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwOTAzL1VNdEhtUUVjRFNqNS5wbmc?x-oss-process=image/format,png" alt=""></p>
<p>根据该时序图编写代码如下：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/**
 * @brief   读取SPI FLASH数据
 * @param   buffer      —— 数据存储区
 * @param   start_addr  —— 开始读取的地址(最大32bit)
 * @param   nbytes      —— 要读取的字节数(最大65535)
 * @retval  成功返回0，失败返回-1
 */</span>
<span class="token keyword">int</span> <span class="token function">W25QXX_Read</span><span class="token punctuation">(</span>uint8_t<span class="token operator">*</span> buffer<span class="token punctuation">,</span> uint32_t start_addr<span class="token punctuation">,</span> uint16_t nbytes<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    uint8_t cmd <span class="token operator">=</span> READ_DATA_CMD<span class="token punctuation">;</span>

    start_addr <span class="token operator">=</span> start_addr <span class="token operator">&lt;&lt;</span> <span class="token number">8</span><span class="token punctuation">;</span>

    <span class="token function">W25QXX_Wait_Busy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

     <span class="token comment" spellcheck="true">/* 使能片选 */</span>
    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_RESET<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">SPI_Transmit</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>cmd<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>HAL_OK <span class="token operator">==</span> <span class="token function">SPI_Transmit</span><span class="token punctuation">(</span><span class="token punctuation">(</span>uint8_t<span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>start_addr<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>HAL_OK <span class="token operator">==</span> <span class="token function">SPI_Receive</span><span class="token punctuation">(</span>buffer<span class="token punctuation">,</span> nbytes<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_SET<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_SET<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="写使能-禁止"><a href="#写使能-禁止" class="headerlink" title="写使能/禁止"></a>写使能/禁止</h2><p>Flash 芯片默认禁止写数据，所以<strong>在向 Flash 写数据之前，必须发送命令开启写使能</strong>，数据手册中给出的时序如下：</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwOTAzLzNBRUk1dm8xUEF0Ni5wbmc?x-oss-process=image/format,png" alt="mark"></p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwOTAzL1hGSmVxVlAyU3ludS5wbmc?x-oss-process=image/format,png" alt="mark"></p>
<p>编写函数如下：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/**
 * @brief    W25QXX写使能,将S1寄存器的WEL置位
 * @param    none
 * @retval
 */</span>
<span class="token keyword">void</span> <span class="token function">W25QXX_Write_Enable</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    uint8_t cmd<span class="token operator">=</span> WRITE_ENABLE_CMD<span class="token punctuation">;</span>

    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_RESET<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">SPI_Transmit</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>cmd<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_SET<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">W25QXX_Wait_Busy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">/**
 * @brief    W25QXX写禁止,将WEL清零
 * @param    none
 * @retval    none
 */</span>
<span class="token keyword">void</span> <span class="token function">W25QXX_Write_Disable</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    uint8_t cmd <span class="token operator">=</span> WRITE_DISABLE_CMD<span class="token punctuation">;</span>

    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_RESET<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">SPI_Transmit</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>cmd<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_SET<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">W25QXX_Wait_Busy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="擦除扇区"><a href="#擦除扇区" class="headerlink" title="擦除扇区"></a>擦除扇区</h2><p>SPI Flash有个特性：</p>
<p><strong>数据位可以由1变为0，但是不能由0变为1。</strong></p>
<p>所以在向 Flash 写数据之前，必须要先进行擦除操作，并且 Flash <strong>最小只能擦除一个扇区</strong>，擦除之后该扇区所有的数据变为 <code>0xFF</code>（即全为1），数据手册中给出的时序如下：</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwOTAzL1BTeGFNYnkxdU11ci5wbmc?x-oss-process=image/format,png" alt="mark"></p>
<p>根据此时序编写函数如下：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/**
 * @brief    W25QXX擦除一个扇区
 * @param   sector_addr    —— 扇区地址 根据实际容量设置
 * @retval  none
 * @note    阻塞操作
 */</span>
<span class="token keyword">void</span> <span class="token function">W25QXX_Erase_Sector</span><span class="token punctuation">(</span>uint32_t sector_addr<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    uint8_t cmd <span class="token operator">=</span> SECTOR_ERASE_CMD<span class="token punctuation">;</span>

    sector_addr <span class="token operator">*</span><span class="token operator">=</span> <span class="token number">4096</span><span class="token punctuation">;</span>    <span class="token comment" spellcheck="true">//每个块有16个扇区，每个扇区的大小是4KB，需要换算为实际地址</span>
    sector_addr <span class="token operator">&lt;&lt;=</span> <span class="token number">8</span><span class="token punctuation">;</span>

    <span class="token function">W25QXX_Write_Enable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment" spellcheck="true">//擦除操作即写入0xFF，需要开启写使能</span>
    <span class="token function">W25QXX_Wait_Busy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">//等待写使能完成</span>

     <span class="token comment" spellcheck="true">/* 使能片选 */</span>
    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_RESET<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">SPI_Transmit</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>cmd<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">SPI_Transmit</span><span class="token punctuation">(</span><span class="token punctuation">(</span>uint8_t<span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>sector_addr<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_SET<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">W25QXX_Wait_Busy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment" spellcheck="true">//等待扇区擦除完成</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="页写入操作"><a href="#页写入操作" class="headerlink" title="页写入操作"></a>页写入操作</h2><p>向 Flash 芯片写数据的时候，因为 Flash 内部的构造，可以按页写入：</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwOTAzL2Rqa2NzMXlJcXI0UC5wbmc?x-oss-process=image/format,png" alt="mark"></p>
<p>页写入的时序如图：</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwOTAzL04wY21ZTFhRWEl0QS5wbmc?x-oss-process=image/format,png" alt="mark"></p>
<p>编写代码如下：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/**
 * @brief    页写入操作
 * @param    dat —— 要写入的数据缓冲区首地址
 * @param    WriteAddr —— 要写入的地址
 * @param   byte_to_write —— 要写入的字节数（0-256）
 * @retval    none
 */</span>
<span class="token keyword">void</span> <span class="token function">W25QXX_Page_Program</span><span class="token punctuation">(</span>uint8_t<span class="token operator">*</span> dat<span class="token punctuation">,</span> uint32_t WriteAddr<span class="token punctuation">,</span> uint16_t nbytes<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    uint8_t cmd <span class="token operator">=</span> PAGE_PROGRAM_CMD<span class="token punctuation">;</span>

    WriteAddr <span class="token operator">&lt;&lt;=</span> <span class="token number">8</span><span class="token punctuation">;</span>

    <span class="token function">W25QXX_Write_Enable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/* 使能片选 */</span>
    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_RESET<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">SPI_Transmit</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>cmd<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">SPI_Transmit</span><span class="token punctuation">(</span><span class="token punctuation">(</span>uint8_t<span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>WriteAddr<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">SPI_Transmit</span><span class="token punctuation">(</span>dat<span class="token punctuation">,</span> nbytes<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">HAL_GPIO_WritePin</span><span class="token punctuation">(</span>W25Q64_CHIP_SELECT_PORT<span class="token punctuation">,</span> W25Q64_CHIP_SELECT_PIN<span class="token punctuation">,</span> GPIO_PIN_SET<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">W25QXX_Wait_Busy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h1 id="6-测试驱动"><a href="#6-测试驱动" class="headerlink" title="6. 测试驱动"></a>6. 测试驱动</h1><p>在 <code>main.c</code> 函数中编写代码，测试驱动：</p>
<p>首先定义两个缓存：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/* Private user code ---------------------------------------------------------*/</span>
<span class="token comment" spellcheck="true">/* USER CODE BEGIN 0 */</span>
uint16_t device_id<span class="token punctuation">;</span>
uint8_t read_buf<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
uint8_t write_buf<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> i<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/* USER CODE END 0 */</span></code></pre>
<p>然后在 main 函数中编写代码：</p>
<pre class=" language-c"><code class="language-c">     <span class="token comment" spellcheck="true">/* USER CODE BEGIN 2 */</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"W25Q64 SPI Flash Test By Mculover666\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    device_id <span class="token operator">=</span> <span class="token function">W25QXX_ReadID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"W25Q64 Device ID is 0x%04x\r\n"</span><span class="token punctuation">,</span> device_id<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/* 为了验证，首先读取要写入地址处的数据 */</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"-------- read data before write -----------\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">W25QXX_Read</span><span class="token punctuation">(</span>read_buf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"[0x%08x]:0x%02x\r\n"</span><span class="token punctuation">,</span> i<span class="token punctuation">,</span> <span class="token operator">*</span><span class="token punctuation">(</span>read_buf<span class="token operator">+</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/* 擦除该扇区 */</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"-------- erase sector 0 -----------\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">W25QXX_Erase_Sector</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/* 再次读数据 */</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"-------- read data after erase -----------\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">W25QXX_Read</span><span class="token punctuation">(</span>read_buf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"[0x%08x]:0x%02x\r\n"</span><span class="token punctuation">,</span> i<span class="token punctuation">,</span> <span class="token operator">*</span><span class="token punctuation">(</span>read_buf<span class="token operator">+</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/* 写数据 */</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"-------- write data -----------\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        write_buf<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">W25QXX_Page_Program</span><span class="token punctuation">(</span>write_buf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/* 再次读数据 */</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"-------- read data after write -----------\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">W25QXX_Read</span><span class="token punctuation">(</span>read_buf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"[0x%08x]:0x%02x\r\n"</span><span class="token punctuation">,</span> i<span class="token punctuation">,</span> <span class="token operator">*</span><span class="token punctuation">(</span>read_buf<span class="token operator">+</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

      <span class="token comment" spellcheck="true">/* USER CODE END 2 */</span></code></pre>
<p><img src="https://img-blog.csdnimg.cn/20200726105809813.png" alt=""><br><img src="https://img-blog.csdnimg.cn/20200726105839150.png" alt=""><br><img src="https://img-blog.csdnimg.cn/2020072610591247.png" alt=""></p>
<p><strong><font color="#FF0000">更多精彩文章及资源，请关注我的微信公众号：『mculover666』。</font></strong></p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwODE0L05RcXQxZVJ4cmwxSy5wbmc?x-oss-process=image/format,png" alt="mark"></p>

            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://www.mculover666.cn" rel="external nofollow noreferrer">Mculover666</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://www.mculover666.cn/posts/858512530/">http://www.mculover666.cn/posts/858512530/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="http://www.mculover666.cn" target="_blank">Mculover666</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/STM32CubeMX-W25Q64/">
                                    <span class="chip bg-color">STM32CubeMX W25Q64</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    
    <div class="social-share" data-sites="qq,qzone,wechat,weibo,douban" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.png" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/we-chat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>
            
        </div>
    </div>

    

    

    

    
    <div class="livere-card card" data-aos="fade-up">
    <!-- 来必力City版安装代码 -->
    <div id="lv-container" class="card-content" data-id="city" data-uid="MTAyMC80Njg0OS8yMzM1MA">
        <script type="text/javascript">
            (function (d, s) {
                let j, e = d.getElementsByTagName(s)[0];
                if (typeof LivereTower === 'function') {
                    return;
                }

                j = d.createElement(s);
                j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
                j.async = true;

                e.parentNode.insertBefore(j, e);
            })(document, 'script');
        </script>
        <noscript>为正常使用来必力评论功能请激活JavaScript。</noscript>
    </div>
    <!-- City版安装代码已完成 -->
</div>
    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="far fa-dot-circle"></i>&nbsp;本篇
            </div>
            <div class="card">
                <a href="/posts/858512530/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/12.jpg" class="responsive-img" alt="STM32CubeMX | 30-使用硬件SPI读写Flash(W25Q64)">
                        
                        <span class="card-title">STM32CubeMX | 30-使用硬件SPI读写Flash(W25Q64)</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本篇详细的记录了如何使用STM32CubeMX配置 STM32G070RBT6 的硬件SPI外设与 SPI Flash 通信（W25Q64）。


1. 准备工作硬件准备
开发板首先需要准备一个开发板，这里我准备的是STM32G070RB的
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-07-26
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/STM32CubeMX/" class="post-category">
                                    STM32CubeMX
                                </a>
                            
                            
                        </span>
                    </div>
                </div>

                
                <div class="card-action article-tags">
                    
                    <a href="/tags/STM32CubeMX-W25Q64/">
                        <span class="chip bg-color">STM32CubeMX W25Q64</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/posts/3295727006/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/2.jpg" class="responsive-img" alt="STM32CubeMX | 29-使用硬件I2C读取甲醛传感器SGP30">
                        
                        <span class="card-title">STM32CubeMX | 29-使用硬件I2C读取甲醛传感器SGP30</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本篇详细的记录了如何使用STM32CubeMX配置STM32L431RCT6的硬件I2C外设读取甲醛传感器SGP30。


1. 准备工作开发板首先需要准备一个开发板，这里我准备的是STM32L4的开发板（BearPi）。
SGP30传感器
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-07-26
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/STM32CubeMX/" class="post-category">
                                    STM32CubeMX
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/STM32CubeMX-SGP30/">
                        <span class="chip bg-color">STM32CubeMX SGP30</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>



    <footer class="page-footer bg-color">
    
    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            <span id="year">2016</span>
            <a href="http://www.mculover666.cn" target="_blank">Mculover666</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">70.7k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2016";
                    var startMonth = "08";
                    var startDate = "28";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
            <span id="icp"><img src="/medias/icp.png" style="vertical-align: text-bottom;" />
                <a href="http://www.beian.miit.gov.cn" target="_blank">陇ICP备19002222号</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/Mculover666" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:mculover666@foxmail.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2412828003" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2412828003" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>





    <a href="https://www.zhihu.com/people/wang-shi-wei-67-38/activities" class="tooltipped" target="_blank" data-tooltip="关注我的知乎: https://www.zhihu.com/people/wang-shi-wei-67-38/activities" data-position="top" data-delay="50">
        <i class="fab fa-zhihu1">知</i>
    </a>



    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Global site tag (gtag.js) - Google Analytics -->


    <!-- Baidu Analytics -->

<script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?6e1efa0988723a1db6356535db2ae6bd";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    

    

    
    <script type="text/javascript" src="/libs/background/ribbon-dynamic.js" async="async"></script>
    
    
    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
